<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="com.sinodata.bsm.common.vo.HealthCalcRule"%>
<%@page import="com.sinodata.bsm.common.constants.ResConstants"%>
<%@ page import="com.sinodata.bsm.center.purview.FunctionConstants"%>
<%@ page import="com.sinodata.bsm.center.purview.UserFunctionValidate"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
	 HealthCalcRule healthRule = (HealthCalcRule)request.getAttribute("healthRule");
 %>
<html>
  <head>
    <title>健康度策略</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="cache-control" content="no-cache"/>
	<meta http-equiv="expires" content="0"/>  
	<%@ include file="../include/common.jsp"%>
	<%@ include file="../include/table.jsp"%>
	<%@ include file="../include/jbox.jsp"%>
	<%@ include file="../include/form.jsp"%>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/default/bsm/sub_menu.css" />
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/progressbar/jquery.progressbar.min.js"></script>
	<style type="text/css">
		.usagebox {border: 1px solid #ccc;width:100px;float:left;margin-left:10px;}
		.usagebox span {height: 16px;}
		.highbar {background: #fff url(${pageContext.request.contextPath}/style/default/bsm/images/bg_usage_red.png) repeat-x left top;}
	</style>
	<script type="text/javascript">
	
	    //算法规则
		var atrithmetic = '<%=healthRule.getArithmetic().intValue() %>';
		$(document).ready(function(){
			$("#childRes").dataTable({
				"bProcessing": true, 
				"bServerSide": true,
				"sServerMethod": 'POST',
				"sAjaxSource":'res-healthy!list.action?resId=${healthRule.id}',
				"bPaginate":false,
				"bSort": true,
				"bFilter":false,
				"sScrollY":$(document).height()-125,
				"sPaginationType": 'full_numbers',
				"aoColumns":[
					{"mDataProp":"checkbox","bSortable": false},
					{"mDataProp":"name","bSortable": false},
					{"mDataProp":"resTypeName","bSortable": false},
					{"mDataProp":"isTrans","bSortable": false},
					{"mDataProp":"weightAndProportion","bSortable": false,"fnCreatedCell":function(nTd, sData, oData, iRow, iCol){
						$(nTd).addClass("weightTd").html("<span style='padding-right:5px;padding-left:5px;'>"+oData.weight+"</span><span  class='progressBar' weightProportion='"+oData.weightProportion+"' name='displayWeight' id='spaceused"+iRow+"'></span>");
					}},
					{"mDataProp":"weight","bSortable": false,"bVisible":false},
					{"mDataProp":"weightProportion","bSortable": false,"bVisible":false},
					{"mDataProp":"id","bVisible":false}
				],
				"oLanguage": { 
					 				"sLengthMenu": "每页 _MENU_ 条",
					 		 		"sZeroRecords": "没有找到记录",
									"sInfo": "当前从 _START_ 到 _END_ 条,共 _TOTAL_ 条记录", 
									"sInfoEmpty": "",
									"oPaginate":{"sFirst":"首页","sPrevious":"上一页","sNext":"下一页","sLast":"尾页"}	
				},
				"fnCreatedRow":function( nRow, aData, iDataIndex ){
					
				},
				"fnInitComplete":function(oSettings,json){
					var weightObs =$('tr').find("td span:[name='displayWeight']");
					weightObs.each(function(index,ob){		
						var weightProportion = $(ob).attr("weightProportion");
						$(ob).progressBar(weightProportion.replace('%',''), {showText: true,barImage:'${pageContext.request.contextPath}/style/default/bsm/images/progressbg_green.gif'});
					});
				}
				
			});
			
			$('#childRes tbody td :checkbox').live('click', function (e) {
    			var nTr = $(this).parents('tr')[0]; 
   		    	showDetailFlag = true;
    			  if ($(nTr).hasClass('row_selected')) {
					  	$(nTr).removeClass('row_selected'); 
				  }else
				  {
				  		$(nTr).addClass('row_selected'); 
				 }
				 
				 if(!$(this).attr("checked")){
				 	$('#allCheck').attr("checked",false);
				 }
    		});
			
			showDetailFlag = false;
			$('#childRes tbody tr').live('click', function (e) {
				if(showDetailFlag){
				 	showDetailFlag = false;
				 	return;
				 }
			
				 //单机行的所有TD
				  var nTds = $('td', this);
			   	 if ( $(this).hasClass('row_selected') ) {
					  	$(this).removeClass('row_selected'); 
					  	//点击全选时，如果在全选的基础上 有一个被取消选择,全选checkedbox不选中
					  	if($('#allCheck').attr("checked"))
					  	{
					  		$('#allCheck').attr("checked",false);
					  	}
				  }else
				  {
						//显示背景色
				  		$(this).addClass('row_selected'); 
				 }
			});
			
			
			
			$("input[name='arithmetic']").live('click',function(event){
				//如果当前选择的算法规则不等于初始算法规则值，则显示提交按钮，否则隐藏。
				if(event.target.value!=atrithmetic){ 
					$('#saveBtnDiv').show();
				}else{
					$('#saveBtnDiv').hide();
				}
			});
			
		});
		
		function allCheck(){
			var allCheck = $('#allCheck').attr("checked");
  	    	var allCheckbox = $("input[name='res_check']");
  	    	if(allCheck)
  	    	{
  	    		allCheckbox.attr("checked",allCheckbox);  //全选
  	    		$('#childRes tbody tr').addClass('row_selected');  //背景色全选
  	    	}else
  	    	{
  	    		allCheckbox.attr("checked",false);   //取消全选
  	    		$('#childRes tbody tr').removeClass('row_selected');  //背景色取消
  	    	}
		}
		
		function openSetWindow(){
			var selected = $("input[name='res_check']:checked");
			if(selected.length>0){
				var url ='iframe:resource/res-healthy!rule.action';
		    	var opt =  {title:'子资源健康度传递规则设置',id:'healthyConfigWindow',width:400,height:140,iframeScrolling:'no',submit:function(v, h, f){},buttons:{}};
		    	getTopWin(window).openjBox(url,opt,'healthyConfigWindow',window);
			}else{
				getTopWin(window).getMessageBox().alert('请选择资源','提示');
			}
		}
		
		/**
		*设置子资源健康度
		*healtyRule是一个json对象
		*/
		set = function(healthyRule){
			var selected = $("input[name='res_check']:checked");
				var healthyRules = new Array();
			var oTable =$('#childRes').dataTable();
		$.each(selected,function(index,ob){
				var tr = $(ob).parents('tr')[0];
				var entity = oTable.fnGetData(tr);
				healthyRules.push("${healthRule.id}#"+entity.id+"#"+healthyRule.isTrans+"#"+healthyRule.weight+"");
			});
			getTopWin(window).getMessageBox().tip("设置中...", 'loading');
			$.ajax({
				type:'post',
				url:'res-healthy!saveHealthTransRule.action',
				data:{rules:healthyRules.toString(),resId:"${healthRule.id}"},
				global:false,
				async:false,
				cache:false,
				dataType:'json',
				success:function(data,textStatus){
					window.setTimeout(function () {
						getTopWin(window).getMessageBox().tip('设置成功。', 'success'); 
						oTable.fnClearTable(); 
						$("#allCheck").attr("checked",false);
					},1500);
				},
				error:function(){
					getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
				}
			});
		}
		
		//恢复默认
		function setDefault(){
			var selected = $("input[name='res_check']:checked");
			var healthyRules = new Array();
			if(selected.length>0){
				var oTable =$('#childRes').dataTable();
				$.each(selected,function(index,ob){
					var tr = $(ob).parents('tr')[0];
					var entity = oTable.fnGetData(tr);
					healthyRules.push("${healthRule.id}#"+entity.id+"#1#5");
				});
				
				var submit = function(v, h, f){
	  	    	  		if (v == 'ok'){
	  	    	  		 	getTopWin(window).getMessageBox().tip("设置中...", 'loading');
	  	    	  		 	$.ajax({
	  	    	  		 		type:'post',
								url:'res-healthy!saveHealthTransRule.action',
								data:{rules:healthyRules.toString(),resId:"${healthRule.id}"},
								global:false,
								async:false,
								dataType:'json',
								success:function(data,textStatus){
									window.setTimeout(function () {
										getTopWin(window).getMessageBox().tip('恢复默认设置成功。', 'success'); 
										oTable.fnClearTable(); 
										$("#allCheck").attr("checked",false);
									},1500);
								},
								error:function(){
									getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
								}
						    });
	  	    	  		 }else if (v == 'cancel'){ 
	  	    	  		 
	  	    	  		 }
	  	    	}
				
				getTopWin(window).getMessageBox().confirm('确定恢复默认设置?','提示',submit);
			}else{
				getTopWin(window).getMessageBox().alert('请选择要恢复默认的资源','提示');
			}
		}
		
		
		function save(){
			var healtyForm = $('#healtyForm').serialize();
				 $.ajax({
					type:'post',
					url:'res-healthy!save.action?id=${healthRule.id}',
					data:healtyForm,
					global:false,
					async:false,
					cache:false,
					dataType:'json',
					success:function(data,textStatus){
						getTopWin(window).getMessageBox().tip('设置成功', 'success',{timeout:1000}); 
						atrithmetic = $("input[name=arithmetic]:checked").val();
					},
					error:function(){
						getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
					}
				});
		}
	</script>
  </head>
  
  <body >
    	<h2 class="title-h2" ><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>资源【${res.name}】健康度设置</h2> 
    	<div id="topDiv">
    		<form action="" id="healtyForm" name="healtyForm">
	    		<table class="table_eidt" style="border-color:#e7e6e7;" >
	    			<tbody>
	    				<tr>
	    					<td class="td_left" style="width:150px;">算法规则</td>
	    					<td class="td_right" style="width:200px;border-right:0px;">
	    						<input type="radio" value="<%=ResConstants.AVG_ARITHMETIC %>" name="arithmetic" <% if(healthRule.getArithmetic().intValue()==ResConstants.AVG_ARITHMETIC){out.println("checked='checked'");} %>  />平均算法
	    						<input type="radio" value="<%=ResConstants.PRE_ARITHMETIC %>" name="arithmetic" <% if(healthRule.getArithmetic().intValue()==ResConstants.PRE_ARITHMETIC){out.println("checked='checked'");} %> />优先算法
	    					</td>  
	    					<td class="td_right"   style="border:0px;">
	    						<div id="saveBtnDiv" style="display:none;border:0px;">
	    							<input type="button" value="提交" onclick="save()"  class="btn-4" />
	    						</div>
	    					</td>
	    				</tr>
	    			</tbody>
	    		</table>
	    	</form>
    	</div>
    	
    	<h2 class="title-h2" onclick="accordionDiv(this)" ><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>子资源健康度传递设置</h2> 
    	<div>
    	  	<%boolean sysManager = UserFunctionValidate.sysFunctionValidate(FunctionConstants.SYSTEM_MANAGER);%>
	   <%if(sysManager){%> 
    		<div class ="toolbar" id="toolbar">
  				<a href="#" onclick="openSetWindow()" class="page_edit" >设置</a>
  				<span class="page_s" >&nbsp;</span>
  				<a href="#" onclick="setDefault()"  class="page_edit" >恢复默认设置</a>
  			</div>
  			<%}%>
    		<table width="100%"  id="childRes" cellpadding="0" cellspacing="0" border="0" class="display">
    			<thead>
    	 			<tr>
    	 				<th width="20px">&nbsp;&nbsp;<input type="checkbox" id="allCheck" onclick="allCheck()" /></th>
    	 				<th width="450px">子资源名称</th>
    	 				<th width="200px">子资源类别</th>
    	 				<th width="80px">是否传递父资源</th>
    	 				<th>权重及比例</th>
    	 			</tr>
    	 		</thead>
    		</table>
    	</div>
  </body>
</html>
